package com.carbon.controller;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.carbon.common.core.controller.BaseController;
import com.carbon.common.core.domain.AjaxResult;
import com.carbon.domain.MaterialReq;
import com.carbon.mapper.MaterialReqMapper;
import com.carbon.service.MaterialReqService;
import com.carbon.vo.SearchParams;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/wms/material")
public class MaterialReqController extends BaseController {
    @Autowired
    private MaterialReqService materialReqService;
    @Resource
    private MaterialReqMapper materialReqMapper;




    @GetMapping("/list")
    public AjaxResult list()
    {
//        startPage();
        List<MaterialReq> list = materialReqService.list();
        return AjaxResult.success(list);
    }

    @GetMapping("/seclist")
    public AjaxResult SearchList(SearchParams params){
        System.out.println(params+"\n\n");
        LambdaQueryWrapper<MaterialReq> lqw=new LambdaQueryWrapper<MaterialReq>();
        String search = params.getSearch();
        String dateTime = params.getDateTime();
        if (ObjectUtil.isNotEmpty(search!=null)) {
            lqw.like(MaterialReq::getOrderId, search).or()
                    .like(MaterialReq::getProposer,search).or()
                    .like(MaterialReq::getAuditor, search).or()
                    .like(MaterialReq::getId,search).or()
                    .like(MaterialReq::getProPlan, search).or()
                    .like(MaterialReq::getProNum,search).or()
                    .like(MaterialReq::getProduct, search).or()
                    .like(MaterialReq::getProSpe, search).or()
                    .like(MaterialReq::getProModel, search);
        }
        if (ObjectUtil.isNotEmpty(dateTime!=null)) {
            lqw.like(MaterialReq::getGetTime, dateTime).or()
                    .like(MaterialReq::getReqTime, dateTime).or()
                    .like(MaterialReq::getAudTime, dateTime).or();
        }
        if (ObjectUtil.isNotEmpty(params.getCreateStatus()!=null)) {
            lqw .eq(MaterialReq::getReqStatus, params.getCreateStatus()).or();
        }
        if (ObjectUtil.isNotEmpty(params.getUpdateStatus()!=null)) {
            lqw .eq(MaterialReq::getAudStatus, params.getUpdateStatus()).or();
//
        }

        List<MaterialReq> records = materialReqMapper.selectList(lqw);
        return AjaxResult.success(records);
    }
    /*
    修改审核状态
     */
    @PostMapping("/audit")
    public AjaxResult update(@RequestBody MaterialReq materialReq)
    {
        materialReq.setAuditor(getUsername());
        MaterialReq materialReq1=new MaterialReq();
        materialReq1.setId(materialReq.getId());
        materialReq1.setAudStatus(materialReq.getAudStatus());
        materialReq1.setAudTime(materialReq.getAudTime());
        materialReq1.setAuditor(materialReq.getAuditor());
        System.out.println(materialReq1);
        materialReqService.updateById(materialReq1);
        return AjaxResult.success();
    }
}
